package com.algrithom.stack.monotonousstack;

import java.util.Arrays;
import java.util.Stack;

class Solution3 {
    
    public static void main(String[] args){
        int[] temperature = {73,74,75,71,69,72,76,73};
        Solution3 solution3 = new Solution3();
        System.out.println(Arrays.toString(solution3.dailyTemperatures(temperature)));
    }
    
    public int[] dailyTemperatures(int[] T){
        int[] result = new int[T.length];
        Arrays.fill(result,0);
        Stack<Integer> stack = new Stack<>();
        for (int index = T.length - 1; index >= 0; index--) {
            while (!stack.isEmpty() && T[stack.peek()] <= T[index]) {
                stack.pop();
            }
            result[index] = stack.isEmpty() ? 0 : stack.peek() - index;
            stack.push(index);
        }
        return result;
    }
}